var gulp=require("gulp");
var gulp_min_css=require("gulp-minify-css");
var gulp_sass=require("gulp-sass");
var gulp_plumber=require("gulp-plumber");
var gulp_tinypng=require("gulp-tinypng");
var gulp_uglify=require("gulp-uglify");
var gulp_autoprefixer=require('gulp-autoprefixer');
var gulp_copy = require('gulp-copy');

//压缩css
gulp.task("mincss",function(){
	//这个任务具体要做的事情就在这里写了
	gulp.src('src/css/**/*.css')
	.pipe(gulp_plumber())
	.pipe(gulp_min_css())
	.pipe(gulp_autoprefixer({
			browsers: ['last 2 versions', 'Android >= 4.0','iOS 7','last 3 Safari versions'],
			cascade: false
		}))
	.pipe(gulp.dest('./dist/css'));
})
//sass转css
gulp.task("sass",function(){
	//这个任务具体要做的事情就在这里写了
	gulp.src('src/scss/**/*.scss')
	.pipe(gulp_plumber())
	.pipe(gulp_sass())
	.pipe(gulp.dest('src/css'));
})
//监听scss
gulp.task("watchscss",function(){
	gulp.watch(
		'./scss/**/*.scss',["mincss"]
	)
})
//压缩图片
gulp.task("minimg",function(){
	gulp.src('./src/img/**/*.*')
	.pipe(gulp_tinypng("rKMyV7Q_V8TX37IbqbeQfeAW3e6AxM18"))
	.pipe(gulp.dest('./dist/img/**'));
})

//压缩js
gulp.task("minjs",function(){
	gulp.src('./src/js/**/*.js')
	.pipe(gulp_uglify())
	.pipe(gulp.dest('./dist/js'));
})

//复制html以及fonts
gulp.task('copy', function() {
    gulp.src("./src/fonts/**/*").pipe(gulp_copy("./dist/fonts"))  
    gulp.src("./src/html/**/*.html").pipe(gulp_copy("dist/html",{prefix:"1"}))
})

//定义浏览器刷新任务
var browser_sync=require("browser-sync").create();
gulp.task('browser-sync', function() {
    browser_sync.init({
        server: {
            baseDir: "./src"
        }
    });
    gulp.watch("src/scss/**/*.scss",["sass"]).on("change",browser_sync.reload);
    gulp.watch("src/*.html").on("change",browser_sync.reload);
});

//定义一键开发模式
gulp.task("dev",["browser-sync"]);

//定义一建生成模式
gulp.task("pro",["minimg","minjs","mincss","copy"]);
